package net.bplaced.streetfighter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import net.bplaced.streetfighter.adapter.MatchAdapter;
import net.bplaced.streetfighter.lib.async.task.MatchLoaderTask;
import net.bplaced.streetfighter.lib.model.Match;
import net.bplaced.streetfighter.lib.utils.CallBack;
import net.bplaced.streetfighter.lib.utils.ServiceReturnPackage;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public class MatchesActivity
        extends Activity {

    private Button searchDateButton;
    private ListView matchList;
    private Calendar selectedDate;

    private Context context;

    private MatchAdapter matchAdapter;

    static final int DATE_DIALOG_ID = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.context = this;

        this.setContentView(R.layout.matches);

        this.matchAdapter = new MatchAdapter(this, new ArrayList<Match>());
        this.matchList = (ListView) this.findViewById(R.id.matchList);
        this.matchList.setAdapter(this.matchAdapter);

        this.searchDateButton = (Button) this.findViewById(R.id.searchDateButton);
        this.searchDateButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                MatchesActivity.this.showDialog(MatchesActivity.DATE_DIALOG_ID);
            }
        });

        final Button startSearchBtn = (Button) this.findViewById(R.id.startSearchButton);
        startSearchBtn.setOnClickListener(this.searchButtonClickListener);

        this.setActualDate();
    }

    /**
     * Deklariert einen Callback, welcher zum Ende des Serviceaurufes aufgerufen wird.
     */
    private final CallBack<ServiceReturnPackage<Match[]>> onFinishedLoading =
            new CallBack<ServiceReturnPackage<Match[]>>() {

                @Override
                public void callbackAction(ServiceReturnPackage<Match[]> result) {

                    if (result != null) {
                        if (result.getContent() != null) {

                            MatchesActivity.this.matchAdapter.clear();
                            MatchesActivity.this.matchAdapter.addAll(result.getContent());

                        } else {

                            Toast.makeText(MatchesActivity.this, result.getErrorMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                };
            };

    /**
     * Sets the displayed date to the actual one.
     */
    private void setActualDate() {

        this.selectedDate = Calendar.getInstance();

        this.updateDisplay();
    }

    /**
     * Creates a DatePickerDialog with the given Date.
     */
    @Override
    protected Dialog onCreateDialog(int id) {

        switch (id) {
            case DATE_DIALOG_ID:

                return new DatePickerDialog(this, this.mDateSetListener, this.selectedDate.get(Calendar.YEAR),
                        this.selectedDate.get(Calendar.MONTH), this.selectedDate.get(Calendar.DAY_OF_MONTH));
        }
        return null;
    }

    /**
     * Updates the displayed date on the view to the selected one.
     */
    private void updateDisplay() {

        this.searchDateButton.setText(new SimpleDateFormat("dd.MM.yyyy").format(this.selectedDate.getTime()));
    }

    /**
     * The callback, when the user "sets" a date on the DatePickerDialog.
     */
    private final DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            MatchesActivity.this.selectedDate.set(year, monthOfYear, dayOfMonth);

            MatchesActivity.this.updateDisplay();
        }
    };

    /**
     * The callback, when the user starts the search.
     */
    private final View.OnClickListener searchButtonClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            final MatchLoaderTask task =
                    new MatchLoaderTask(MatchesActivity.this.context, MatchesActivity.this.getResources().getString(
                            R.string.matches_progress_message), MatchesActivity.this.onFinishedLoading);
            task.execute(MatchesActivity.this.selectedDate.getTime());
        }
    };
}